import os
import os.path as osp

import cv2
import numpy as np
from tqdm import tqdm


if __name__ == "__main__":
    src_pts = [
        [340, 166],
        [867, 189],
        [356, 636],
        [859, 605]
    ]
    dst_pts = [
        [80, 80],
        [1100, 80],
        [80, 680],
        [1100, 680]
    ]

    src_pts = np.float32(src_pts)
    dst_pts = np.float32(dst_pts)

    # 计算透视变换矩阵
    perspective_matrix = cv2.getPerspectiveTransform(src_pts, dst_pts)


    # 
    image = cv2.imread("undistort_image.jpg")
    h, w = image.shape[:2]
    print(h, w)
    # 透视变换
    dst_image = cv2.warpPerspective(image, perspective_matrix, (w, h), flags=cv2.BORDER_CONSTANT, borderMode=cv2.BORDER_CONSTANT, borderValue=0)

    # show
    cv2.imshow("image", image)
    cv2.imshow("dst_image", dst_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    cv2.imwrite("perspective_image.jpg", dst_image)

    pass